home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / gnu / gawk / cawf2st.zoo / fontfilt.c < prev    next >
C/C++ Source or Header  |  1992-04-12  |  25KB  |  811 lines

  1. /*
  2.  * fontfilt.c - cawf post-processing font filter
  3.  *
  4.  * V. Abell
  5.  * Purdue University Computing Center
  6.  *
  7.  * Fontfilt filters output from cawf, the C version of Henry Spencer's
  8.  * Amazingly Workable (text) Formatter, awf, to produce printer-specific
  9.  * codes for bold and italic characters.  (Cawf provides modest support
  10.  * for documents formatted with nroff's man(7) and ms(7) macros.)
  11.  *
  12.  * Fontfilt is based on work by and suggestions from Chet Creider
  13.  * <creider@csd.uwo.ca>.
  14.  */
  15.  
  16.  
  17. /*
  18.  *      Copyright (c) 1991 Purdue University Research Foundation,
  19.  *      West Lafayette, Indiana 47907.  All rights reserved.
  20.  *
  21.  *      Written by Victor A. Abell <abe@mace.cc.purdue.edu>, Purdue
  22.  *      University Computing Center.  Not derived from licensed software;
  23.  *      derived from work by Chet Creider <creider@csd.uwo.ca>.
  24.  *
  25.  *      Permission is granted to anyone to use this software for any
  26.  *      purpose on any computer system, and to alter it and redistribute
  27.  *      it freely, subject to the following restrictions:
  28.  *
  29.  *      1. The author is not responsible for any consequences of use of
  30.  *         this software, even if they arise from flaws in it.
  31.  *
  32.  *      2. The origin of this software must not be misrepresented, either
  33.  *         by explicit claim or by omission.  Credits must appear in the
  34.  *         documentation.
  35.  *
  36.  *      3. Altered versions must be plainly marked as such, and must not
  37.  *         be misrepresented as being the original software.  Credits must
  38.  *         appear in the documentation.
  39.  *
  40.  *      4. This notice may not be removed or altered.
  41.  */
  42.  
  43.  
  44. /*
  45.  * Usage:
  46.  *
  47.  *      fontfilt [-c config] [-d device] [-f font] [file(s)]
  48.  *
  49.  *      where:
  50.  *
  51.  *              -c config       specifies an alternate configuration file
  52.  *                              (default directory = CAWFLIB definition or
  53.  *                                                   CAWFLIB env. variable)
  54.  *
  55.  *              -d device       specifies the output device
  56.  *
  57.  *              -f font         specifies the font to be used on the
  58.  *                              output device
  59.  *
  60.  *              file(s)         the path(s) to the file(s) containing cawf,
  61.  *                              -fe format output
  62.  *
  63.  * Cawf's font ESCape mode must be used - e. g.,
  64.  *
  65.  *      % cawf -fe -man cawf.1 | fontfilt -dlj3 -flg12
  66.  */
  67.  
  68.  
  69. /*
  70.  * See fontfilt.cf for a list of supported devices and fonts, or use
  71.  * the -h (help) option.  The default device is the last device named
  72.  * in fontfilt.cf.
  73.  */
  74.  
  75. #include <stdio.h>
  76. #ifdef  STDLIB
  77. #include <stdlib.h>
  78. #endif
  79. #ifdef  UNIX
  80. #ifdef  USG
  81. #include <string.h>
  82. #else
  83. #include <strings.h>
  84. #endif
  85. #else
  86. #include <string.h>
  87. #endif
  88. #include <ctype.h>
  89. #include <sys/types.h>
  90. #include <sys/stat.h>
  91.  
  92. #include "cawflib.h"
  93.  
  94.  
  95. /*
  96.  * Local definitions
  97.  */
  98.  
  99. #define BOLD            'B'
  100. #define CONFIG          "fontfilt.cf"
  101. #define ESC             0x1b
  102. #define ITALIC          'I'
  103. #define MAXLINE         512
  104. #define ROMAN           'R'
  105.  
  106.  
  107. /*
  108.  * Local global variables
  109.  */
  110.  
  111. static char *Besc = NULL;       /* bold font escape string pointer */
  112. static char *Conf = NULL;       /* configuration file path */
  113. static char *Defdev = NULL;     /* default device name (last name of
  114.                                  * fontfilt.cf */
  115. static char Font = ROMAN;       /* current font */
  116. static char *Iesc = NULL;       /* italic font escape string pointer */
  117. static char *Pname;             /* program name */
  118. static char *Resc = NULL;       /* roman font escape string pointer */
  119.  
  120.  
  121. /*
  122.  * Structure for font definitions
  123.  */
  124.  
  125. struct font {
  126.         char *nm;               /* font name */
  127.         char *fi;               /* font initialization character sequence */
  128.         struct font *next;      /* next font fot this device */
  129. };
  130.  
  131.  
  132. /*
  133.  * Device structure
  134.  */
  135.  
  136. struct dev {
  137.         char *nm;               /* device name */
  138.         struct font *f;         /* supported fonts */
  139.         char *b;                /* bold font ESCape sequence */
  140.         char *i;                /* italic font ESCape sequence */
  141.         char *r;                /* Roman font ESCape sequence */
  142.         struct dev *next;       /* next device */
  143. } *Dp = NULL;
  144.  
  145.  
  146. /*
  147.  * Externals
  148.  */
  149.  
  150. extern char *optarg;                    /* getopt(3) argument pointer */
  151. extern int optind;                      /* getopt(3) index */
  152.  
  153.  
  154. /*
  155.  * Function definitions
  156.  */
  157.  
  158. static char *Convstr();
  159. static int Convfont(), Readcf();
  160. static void Bold(), Getec(), Italic(), Roman();
  161.  
  162. #ifndef STDLIB
  163. char *getenv(), *malloc(), *strchr(), *strrchr();
  164. #endif
  165.  
  166.  
  167. /*
  168.  * Main program
  169.  */
  170.  
  171. main(argc, argv)
  172.         int argc;                       /* argument count */
  173.         char *argv[];                   /* argument pointers */
  174. {
  175.         register int c;                 /* character buffer */
  176.         char *cnm = NULL;               /* config file name */
  177.         char *dnm = NULL;               /* device name */
  178.         struct dev *dp;                 /* device */
  179.         char *fnm = NULL;               /* font name */
  180.         int err = 0;                    /* argument error count */
  181.         int fc, fx;                     /* file count and index */
  182.         struct font *fp;                /* font pointer */
  183.         FILE *fs;                       /* file stream */
  184.         int help = 0;                   /* -h status */
  185.         struct stat sbuf;               /* file stat() buffer */
  186.         char *sep;                      /* separator pointer */
  187. /*
  188.  * Save program name.
  189.  */
  190.         if ((Pname = strrchr(argv[0], '\\')) != NULL)
  191.                 Pname++;
  192.         else if ((Pname = strrchr(argv[0], '/')) != NULL)
  193.                 Pname++;
  194.         else
  195.                 Pname = argv[0];
  196. /*
  197.  * Process options.
  198.  */
  199.         while ((c = getopt(argc, argv, "c:d:f:h")) != EOF) {
  200.                 switch (c) {
  201.         /*
  202.          * -c config -- specify configuration file
  203.          */
  204.                 case 'c':
  205.                         if (cnm != NULL) {
  206.                                 (void) fprintf(stderr,
  207.                                         "%s: duplicate config name\n", Pname);
  208.                                 err = 1;
  209.                         } else
  210.                                 cnm = optarg;
  211.                         break;
  212.         /*
  213.          * -d device -- specify device
  214.          */
  215.                 case 'd':
  216.                         if (dnm != NULL) {
  217.                                 (void) fprintf(stderr,
  218.                                         "%s: duplicate device name\n", Pname);
  219.                                 err = 1;
  220.                         } else
  221.                                 dnm = optarg;
  222.                         break;
  223.         /*
  224.          * -f font -- specify font
  225.          */
  226.                 case 'f':
  227.                         if (fnm != NULL) {
  228.                                 (void) fprintf(stderr,
  229.                                         "%s: duplicate font name\n", Pname);
  230.                                 err = 1;
  231.                         } else
  232.                                 fnm = optarg;
  233.                         break;
  234.         /*
  235.          * -h -- request help
  236.          */
  237.                 case 'h':
  238.                         help++;
  239.                         break;
  240.         /*
  241.          * unknown option
  242.          */
  243.                 case '?':
  244.                         err = 1;
  245.                 }
  246.         }
  247. /*
  248.  * Handle file arguments.
  249.  */
  250.         if (optind >= argc)
  251.                 fc = 0;
  252.         else {
  253.                 fc = argc;
  254.                 for (fx = optind; fx < fc; fx++) {
  255.                         if (stat(argv[fx], &sbuf) != 0) {
  256.                                 (void) fprintf(stderr, "%s: can't find %s\n",
  257.                                         Pname, argv[fx]);
  258.                                 err++;
  259.                         }
  260.                 }
  261.         }
  262. /*
  263.  * Read configuration file.
  264.  */
  265.         if (Readcf(cnm) == 0)
  266.                 err++;
  267. /*
  268.  * Validate device name.
  269.  */
  270.         if (dnm == NULL)
  271.                 dnm = Defdev;
  272.         for (dp = Dp; dp; dp = dp->next) {
  273.                 if (strcmp(dnm, dp->nm) == 0)
  274.                         break;
  275.         }
  276.         if (dp) {
  277.                 if (fnm == NULL && dp->f)
  278.                         fnm = (dp->f)->nm